﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
namespace WebHelper.Pages
{
    public class Cookie
    {
        ///  
        /// Cookie作用域 
        ///  
        public enum CookieDomain
        {
            Private,		///本网站Cookie 
            Public			///全网Cookie 
        }
        public static string DomainStr = "www.5151feel.com";

        #region Cookie操作类
        ///  
        /// 设置本网站Cookie值 
        ///  
        /// Cookie作用域 
        /// Cookie名称 
        /// Cookie值 
        public static void WriteCookie(string cookieName, string cookieValue)
        {
            CookieDomain cookieDomain = CookieDomain.Private;
            DateTime cookieExpire = DateTime.MaxValue;
            WriteCookie(cookieDomain, cookieName, cookieValue, cookieExpire);
        }
        ///  
        /// 设置Cookie值 
        ///  
        /// Cookie作用域 
        /// Cookie名称 
        /// Cookie值 
        public static void WriteCookie(CookieDomain cookieDomain, string cookieName, string cookieValue)
        {
            DateTime cookieExpire = DateTime.MaxValue;
            WriteCookie(cookieDomain, cookieName, cookieValue, cookieExpire);
        }
        ///  
        /// 设置Cookie值 
        ///  
        /// Cookie作用域 
        /// Cookie名称 
        /// Cookie值 
        /// Cookie过期时间 
        public static void WriteCookie(CookieDomain cookieDomain, string cookieName, string cookieValue, int days)
        {
            DateTime cookieExpire = DateTime.Now.AddDays(days);
            WriteCookie(cookieDomain, cookieName, cookieValue, cookieExpire);
        }
        ///  
        /// 设置Cookie值 
        ///  
        /// Cookie作用域 
        /// Cookie名称 
        /// Cookie值 
        /// Cookie过期时间 
        public static void WriteCookie(CookieDomain cookieDomain, string cookieName, string cookieValue, DateTime cookieExpire)
        {

            cookieValue = System.Web.HttpUtility.UrlEncode(cookieValue);
            //cookieName = cookieDomain.ToString() + cookieName;
            cookieName = System.Web.HttpUtility.UrlEncode(cookieName);
            HttpCookie cookie = new HttpCookie(cookieName.ToLower(), cookieValue);
           // cookie.Domain = HttpContext.Current.Request.Url.Host;
            if (!cookieExpire.Equals(DateTime.Now))
                cookie.Expires = cookieExpire;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        ///  
        /// 取得本网站指定Cookie值，没有值返回null 
        ///  
        /// 指定Cookie名称 
        ///  
        public static string GetCookie(string cookieName)
        {
            return GetCookie(CookieDomain.Private, cookieName, null);
        }
        ///  
        /// 取得本网站指定Cookie值 
        ///  
        /// 指定Cookie名称 
        /// 默认值 
        ///  
        public static string GetCookie(string cookieName, string defValue)
        {
            return GetCookie(CookieDomain.Private, cookieName, defValue);
        }
        ///  
        /// 取得指定Cookie值 
        ///  
        /// 指定Cookie名称 
        /// 默认值 
        ///  
        public static string GetCookie(CookieDomain cookieDomain, string cookieName, string defValue)
        {
            if (string.IsNullOrEmpty(cookieName))
                return defValue;
            //cookieName = cookieDomain.ToString() + cookieName;
            cookieName = cookieName.ToLower();
            cookieName = System.Web.HttpUtility.UrlEncode(cookieName);
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                return System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[cookieName].Value);
            }
            else
                return defValue;
        }

        public static List<string> GetCookieNames(CookieDomain cookieDomain)
        {
            List<string> l = new List<string>();
            foreach (string cookiename in HttpContext.Current.Request.Cookies.AllKeys)
            {
                if (cookiename.StartsWith(cookieDomain.ToString().ToLower()) && cookiename.Length > cookieDomain.ToString().Length)
                {
                    l.Add(cookiename.Substring(cookieDomain.ToString().Length + 1));
                }
            }
            return l;
        }
        public static int GetCookieCount(CookieDomain cookieDomain)
        {
            List<string> l = GetCookieNames(cookieDomain);
            return l.Count;
        }
        ///  
        /// 删除本网站指定Cookie 
        ///  
        ///  
        public static void Clear(string cookiename)
        {
            Clear(CookieDomain.Private, cookiename);
        }
        ///  
        /// 删除指定网站指定Cookie 
        ///  
        ///  
        ///  
        public static void Clear(CookieDomain cookieDomain, string cookiename)
        {
            WriteCookie(cookieDomain, cookiename, null, DateTime.MinValue);
        }
        ///  
        /// 清除指定域的Cookie 
        ///  
        /// 指定域 
        public static void Clear(CookieDomain cookieDomain)
        {
            foreach (string cookiename in HttpContext.Current.Request.Cookies.AllKeys)
            {
                WriteCookie(cookieDomain, cookiename, null, DateTime.MinValue);
            }
        }
        ///  
        /// 清除所有域的Cookie 
        ///  
        public static void Clear()
        {
            Clear(CookieDomain.Private);
            Clear(CookieDomain.Public);
        }
        ///  
        /// 取得对应CookieDomain的作用域 
        ///  
        ///  
        ///  
        public static string GetCookieDomain(CookieDomain cookiedomain)
        {
            switch (cookiedomain)
            {
                case CookieDomain.Private:
                    string tmp = HttpContext.Current.Request.Url.Host;
                    if (tmp == "user." + DomainStr)
                        tmp = "." + DomainStr;
                    return tmp;
                    break;
                case CookieDomain.Public:
                    return "." + DomainStr;
                    break;
                default:
                    return "." + DomainStr;
                    break;

            }
        }
        #endregion

        #region 旧的Cookie类
        /* 
		public static void WriteCookie(string strName, string strValue) 
		{ 
			HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 
			if (cookie == null) 
			{ 
				cookie = new HttpCookie(strName); 
			} 
			cookie.Value = strValue; 
			HttpContext.Current.Response.AppendCookie(cookie); 
		} 
 
		///  
		/// 写cookie值 
		///  
		/// 名称 
		/// 值 
		public static void WriteCookie(string strName, string strValue, int expires) 
		{ 
			HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 
			if (cookie == null) 
			{ 
				cookie = new HttpCookie(strName); 
			} 
			cookie.Value = strValue; 
			cookie.Expires = DateTime.Now.AddMinutes(expires); 
			HttpContext.Current.Response.AppendCookie(cookie); 
		} 
 
		///  
		/// 读cookie值 
		///  
		/// 键 
		/// 默认的返回值 
		/// 名称 
		/// cookie值 
		public static string GetCookie(string strName, string defValue) 
		{ 
			if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null) 
			{ 
				return HttpContext.Current.Request.Cookies[strName].Value; 
			} 
 
			return defValue; 
		} 
		#endregion 
 
		#region 有子键的操作 
		///  
		/// 写cookie值 
		///  
		/// 键 
		/// 名称 
		/// 值 
		public static void WriteCookie(CookieKey strDomain, string strName, string strValue) 
		{ 
			HttpCookie cookie = new HttpCookie(strName, strValue); 
			cookie.Domain = SetDomain(strDomain); 
			HttpContext.Current.Response.AppendCookie(cookie); 
		} 
 
		///  
		/// 读cookie值 
		///  
		/// 键 
		/// 名称 
		/// cookie值 
		public static string GetCookie(CookieKey strDomain, string strName, string defValue) 
		{ 
			string domain = SetDomain(strDomain); 
			if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null) 
			{ 
				return HttpContext.Current.Request.Cookies[strName].Value; 
			} 
			return defValue; 
		} 
 
		private static string SetDomain(CookieKey strDomain) 
		{ 
			switch (strDomain) 
			{ 
				case CookieKey.www: 
					return "www" + DomainStr; 
				case CookieKey.bbs: 
					return "bbs" + DomainStr; 
				case CookieKey.video: 
					return "video" + DomainStr; 
				case CookieKey.blog: 
					return "blog" + DomainStr; 
				case CookieKey.user: 
					return DomainStr; 
				case CookieKey.group: 
					return "group" + DomainStr; 
				default: 
					return DomainStr; 
			} 
		} 
		 */
        #endregion
    }
}

